<template>
  <div>
    <!--搜索框-->
    <el-form :inline="true" :model="searchForm">
      <el-form-item>
        <el-input
            v-model="searchForm.imageName"
            placeholder="名称"
            clearable>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getImageList()">搜索</el-button>
        <el-button type="primary" @click="dialogFormVisible = true" v-if="hasAuth('sys:user:save')">新增</el-button>
        <el-popconfirm title="确定要删除这些记录吗？" @confirm="delHandle(null)" style="margin-left: 10px;"
                       v-if="hasAuth('sys:user:delete')">
          <el-button type="danger" slot="reference" :disabled="delBtnStu">批量删除</el-button>
        </el-popconfirm>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table
        ref="multipleTable"
        border
        stripe
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%;"
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          prop="id"
          label="编号"
          width="55">
      </el-table-column>
      <el-table-column
          label="壁纸"
          width="150">
        <template slot-scope="scope">
          <img
              style="width: 100px; height: 100px"
              :src="scope.row.imageUrl"
              preview
              :preview-text="scope.row.imageName">
          </img>
        </template>
      </el-table-column>
      <el-table-column
          prop="imageName"
          label="壁纸名"
          width="120">
      </el-table-column>
      <el-table-column
          label="所属分类"
          width="100">
        <template slot-scope="scope">
          <el-tag style="margin-right: 5px;" size="small">
            {{ scope.row.typeName }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="imageSize"
          width="100px"
          label="分辨率（宽*高）">
      </el-table-column>
      <el-table-column
          prop="downloads"
          width="100px"
          label="下载次数">
      </el-table-column>
      <el-table-column
          prop="createTime"
          width="150px"
          label="创建时间">
      </el-table-column>
      <el-table-column
          width="150px"
          prop="updateTime"
          label="修改时间">
      </el-table-column>
      <el-table-column
          width="160px"
          label="操作">
        <template slot-scope="scope">
          <el-divider direction="vertical"></el-divider>
          <el-button type="text" @click="editHandle(scope.row.id)">编辑</el-button>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm title="确定要删除这条记录吗？" @confirm="delHandle(scope.row.id)">
            <el-button type="text" slot="reference">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--页码-->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
    <el-dialog title="壁纸信息" :visible.sync="dialogFormVisible" width="600px"
               :before-close="handleClose">
      <el-form :model="editForm" :rules="editFormRules" ref="editForm">
        <el-form-item label="壁纸名" prop="imageName" label-width="100px">
          <el-input v-model="editForm.imageName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="请上传壁纸" prop="imageUrl" label-width="100px">
          <el-upload
              ref="upload"
              class="avatar-uploader"
              action="http://localhost:8081/upload"
              :show-file-list="false"
              :on-success="uploadImage">
            <img v-if="editForm.imageUrl" :src="editForm.imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="分类" prop="typeId" label-width="100px">
          <el-select v-model="editForm.typeId" clearable placeholder="请选择">
            <el-option
                v-for="item in typeList"
                :key="item.id"
                :label="item.typeName"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('editForm')">取 消</el-button>
        <el-button type="primary" @click="submitEditForm('editForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "User",
  data() {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      disabled: false,
      typeList: [],
      srcList: [
        'https://blog-icatwms.oss-cn-beijing.aliyuncs.com/2022/05-07/28ec424aa0984540b756426f2221e5d738164d1367b440d58b7ed77c702f01db219004018cc2414e83dd5aafd6c163d9.jpg'
      ],
      userType: 'success',
      searchForm: {
        imageName: ''
      },
      editForm: {},
      editFormRules: {
        imageName: [
          {required: true, message: '请输入图片名', trigger: 'blur'}
        ],
        imageUrl: [
          {required: true, message: '请选择图片', trigger: 'blur'}
        ],
        typeId: [
          {required: true, message: '请选择分类', trigger: 'blur'}
        ]
      },
      current: 1,
      total: 0,
      size: 10,
      dialogFormVisible: false,
      tableData: [],
      multipleSelection: [],
      delBtnStu: true,
    }
  },
  methods: {
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleDownload(file) {
      console.log(file);
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(rows) {
      this.multipleSelection = rows;
      this.delBtnStu = rows.length == 0
    },
    getTypeList() {
      this.$axios.get('/type/list').then(res => {
        console.log('分类' + res)
        this.typeList = res.data.data
        console.log(res.data.data)
      })
    },
    getImageList() {
      this.$axios.get('/image', {
        params: {
          name: this.searchForm.imageName,
          current: this.current,
          size: this.size
        }
      }).then(res => {
        console.log(res.data.data.records)
        this.tableData = res.data.data.records
        this.current = res.data.data.current
        this.size = res.data.data.size
        this.total = res.data.data.total
      })
    },
    handleSizeChange(val) {
      this.size = val
      this.getImageList()
    },
    handleCurrentChange(val) {
      this.current = val
      this.getImageList()
    },
    submitEditForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post('/image/' + (this.editForm.id ? "update" : "save"), this.editForm)
              .then(res => {
                console.log(res.data)
                this.resetForm(formName)
                this.$message({
                  showClose: true,
                  message: '恭喜你，操作成功',
                  type: 'success',
                  onClose: () => {
                    this.getImageList()
                  }
                });
              })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    editHandle(id) {
      console.log(id)
      this.$axios.get("/image/" + id).then(res => {
        this.editForm = res.data.data
        console.log(res.data.data)
        console.log(this.editForm)
        this.dialogFormVisible = true
      })
    },
    delHandle(id) {
      var ids = []
      id ? ids.push(id) : this.multipleSelection.forEach(row => {
        ids.push(row.id)
      })
      console.log(ids)
      this.$axios.post("/image/delete", ids).then(res => {
        this.$message({
          showClose: true,
          message: '恭喜你，操作成功',
          type: 'success',
          onClose: () => {
            this.getImageList()
          }
        });
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$refs.upload.clearFiles()
      this.editForm = {}
      this.dialogFormVisible = false
    },
    handleClose(done) {
      done();
      this.resetForm('editForm')
    },
    uploadImage(res) {
      console.log(res)
      this.editForm.imageUrl=res.data
      this.$message({
        showClose: true,
        message: '恭喜你，图片上传成功',
        type: 'success'
      });
    }
  }
  ,
  created() {
    this.getImageList()
    this.getTypeList()
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
